package cn.mulanbay.admin.api.persistent.service;

import cn.mulanbay.common.bean.PageSearch;
import cn.mulanbay.common.exception.PersistentException;
import cn.mulanbay.common.util.StringUtil;
import cn.mulanbay.persistent.dto.RoleFunctionDto;
import cn.mulanbay.persistent.dto.UserRoleDto;
import cn.mulanbay.persistent.entity.Role;
import cn.mulanbay.persistent.entity.RoleFunction;
import cn.mulanbay.persistent.entity.UserRole;
import cn.mulanbay.persistent.mapper.RoleFunctionMapper;
import cn.mulanbay.persistent.mapper.RoleMapper;
import cn.mulanbay.persistent.mapper.UserRoleMapper;
import cn.mulanbay.persistent.query.QueryBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleService extends ServiceImpl<RoleMapper, Role>  {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleFunctionMapper roleFunctionMapper;

    /**
     * 获取列表
     *
     * @param us
     * @return
     */
    public Page<Role> getDataList(PageSearch us){
        Page<Role> pp = new Page<>(us.getPage(),us.getPageSize());
        QueryWrapper qw = QueryBuilder.build(us);
        pp.addOrder(OrderItem.asc("order_index"));
        if(us.getPage()<0){
            List<Role> list = baseMapper.selectList(qw);
            pp.setRecords(list);
        }else{
            pp = baseMapper.selectPage(pp,qw);
        }
        return pp;
    }

    /**
     * 获取用户的角色编号
     * @param userId
     * @return
     */
    public Long getUserRoleId(Long userId){
        return userRoleMapper.getUserRoleId(userId);
    }

    public List<String> selectRoleFPermsList(Long roleId){
        return userRoleMapper.selectRoleFPermsList(roleId);
    }

    /**
     * 保存用户角色
     * @param userId
     * @param roleIds
     */
    @Transactional(rollbackFor = PersistentException.class)
    public void saveUserRole(Long userId,String roleIds){
        userRoleMapper.deleteUserRole(userId);
        if (StringUtil.isNotEmpty(roleIds)) {
            String[] ids = roleIds.split(",");
            for (String s : ids) {
                UserRole rf = new UserRole();
                rf.setUserId(userId);
                rf.setRoleId(Long.valueOf(s));
                userRoleMapper.insert(rf);
            }
        }
    }

    /**
     * 保存角色功能点
     * @param roleId
     * @param functionIds
     */
    @Transactional(rollbackFor = PersistentException.class)
    public void saveRoleFunction(Long roleId,String functionIds){
        roleFunctionMapper.deleteRoleFunction(roleId);
        if (StringUtil.isNotEmpty(functionIds)) {
            String[] ids = functionIds.split(",");
            for (String s : ids) {
                RoleFunction rf = new RoleFunction();
                rf.setRoleId(roleId);
                rf.setFunctionId(Long.valueOf(s));
                roleFunctionMapper.insert(rf);
            }
        }
    }

    /**
     * 获取角色功能点
     * @param roleId
     * @return
     */
    public List<RoleFunctionDto> selectRoleFunctionBeanList(Long roleId){
        return roleFunctionMapper.selectRoleFunctionBeanList(roleId);
    }

    public List<UserRoleDto> getUserRoleList( Long userId){
        return userRoleMapper.getUserRoleList(userId);
    }

}
